package com.wuwei.aliocr.jiwu;

import com.wuwei.aliocr.utils.HttpUtils;
import lombok.*;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.GzipDecompressingEntity;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Setter
@Getter
@Component
public class GetJuanHao {

    @Resource
    private GetTiHao getTiHao;

    @Resource
    private PoJieQuestion poJieQuestion;

    private Map<String,String> header = new HashMap<>();

    private List<String> keys = new ArrayList<>();


    /**
     * 添加请请求头，该请求头用于https://www.airacm.com/learning.php?a=reviews请求
     * 这里注意，请求体保存在txt文件中，文件中不要有Content-Length=76这个参数
     */
    @SneakyThrows
    public void getHead() {
        this.header = new HashMap<>();
        this.keys = new ArrayList<>();
        String fileName = "src\\main\\resources\\GetJuanHao\\header.txt";
        FileReader fileReader = new FileReader(fileName);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String line = bufferedReader.readLine();
        while (line!=null){
//            System.out.println(line);
            if (line.length() > 1) {
                int i = line.indexOf(":");
//                System.out.println(line.substring(0,i));
                this.keys.add(line.substring(0,i));
//                System.out.println(line.substring(i+2));
                this.header.put(line.substring(0,i),line.substring(i + 2));
            }
            line = bufferedReader.readLine();
        }
    }





    /**
     * 模拟浏览器的行为，当点击回顾试卷时，此时会从浏览器中获取所作的所有题目卷号
     */
    @SneakyThrows
    public void getJuanHao(){
        ArrayList<String> list = new ArrayList<>();
        //这里用于设置一共有多少页题，比如当你有9页题的时候，设置为9+1=10
        for (int i = 1; i < 10; i++) {
            list.add(String.valueOf(i));
        }
        this.getHead();
        //这里这个i表示第几页,比如当你要从第3页，这里的i设置为3-1=2，i=2
//        for (int i = 1; i < list.size(); i++) {
        for (int i = 2; i < list.size(); i++) {
            Map<String,String> query = new HashMap<>();
            query.put("a","reviews");
            query.put("page",list.get(i));
            HttpResponse post = HttpUtils.doGet("https://www.airacm.com", "/learning.php", "GET", header, query);
            HttpEntity entity = post.getEntity();
            if (post.containsHeader(HTTP.CONTENT_ENCODING)) {
                entity = new GzipDecompressingEntity(entity);  // Gzip解压缩
            }
            if (entity != null) {
                String responseString = EntityUtils.toString(entity);
                this.deCodeHtml(responseString);
                // 处理响应体内容
            }
            Thread.sleep(3000);
        }
    }

    /**
     * 解析浏览器发来的数据
     * @param responseString
     */
    @SneakyThrows
    private void deCodeHtml(String responseString) {
        Document doc = Jsoup.parse(responseString);
        Elements elements = doc.select("tr.tr3");
        //这里这个i表示从上到下第几套题
        for (int i = 1; i < elements.size(); i++) {
            Element element = elements.get(i);
            String juanHao = element.child(0).text();
            String subject = element.child(1).text();
            //todo 现在暂时做不了英语，英语要重写getTestPager方法
            if ("综合阅读".equals(subject)){
                continue;
            }
            if (!"".equals(subject)) {
                System.out.println(subject);
                poJieQuestion.setSubject(subject);
            }

            getTiHao.getTestPager(juanHao);
            Thread.sleep(3000);
        }


//

    }
}
